package com.ebt.m.widget.indemnify;

import android.graphics.Color;
import android.graphics.Paint;
import android.util.Log;

import com.ebt.m.widget.indemnify.chart.EbtIndemnifyA;

import org.achartengine.chart.PointStyle;

import java.io.Serializable;

/**
 * 利益图数据
 *
 * @author Allen.li
 */
public class IndemnifyConfig implements Serializable {
    /**
     * serialVersionUID
     */
    private static final long serialVersionUID = 1L;
    /**
     * 是否单个利益图
     */
    private boolean isSingle = false;
    private String[] titles;
    private int[] colors;
    private PointStyle[] pointStyles;
    private int[] shapeTypes;
    private String[] sounds;
    private String[] descs;

    private int axesColor = Color.GRAY;
    private int labelsColor = Color.LTGRAY;
    private int marginsColor = Color.BLACK;
    /**
     * 一个二维数组，第一维存储着一个包含x,y两个元素的一维数组
     */
    private Object[][] xyVaules;
    private ItemOnSelectedListener itemOnSelectedListener;// 保障条目被选择的监听
    /**全局变量-声音是否自动播放*/
//	public static boolean soundAutoPlay = false;
    /**
     * 是否取最大取值范围
     */
    private boolean isMaxRange = true;
    /**
     * 初始化时的xy坐标范围
     */
    private double[] initialRange = new double[4];
    /**
     * xy坐标范围
     */
    private double[] range = new double[4];

    private int xInitialMinAge;
    private int xInitialMaxAge;
    private double xRangeMinDouble;
    private double xRangeMaxDouble;

    private double yInitialMinDouble;
    private double yInitialMaxDouble;
    private double yRangeMinDouble;
    private double yRangeMaxDouble;

    /**
     * 是否可见
     */
    private boolean isShow[] = null;
    /**
     * 条形图宽度
     */
    private int barSpacing = EbtIndemnifyA.BarSpacing;
    /**
     * 横轴坐标数目
     */
    private int xLabels = EbtIndemnifyA.XLabels;
    /**
     * x坐标间隔年
     */
    private int intervalAge = 1;

    private boolean isShowFloat = false;

    public static final String X_TITLE_AGE = "保单年度末年龄";
    /**
     * 横轴标题
     */
    private String xTitle = X_TITLE_AGE;

    /**
     * 纵轴标题
     */
    private String yTitle = "金额（元）";

    public String getyTitle() {
        return yTitle;
    }

    public void setyTitle(String yTitle) {
        this.yTitle = yTitle;
    }


    public String getxTitle() {
        return xTitle;
    }

    public void setxTitle(String xTitle) {
        this.xTitle = xTitle;
    }


    public String[] getTitles() {
        return titles;
    }

    public void setTitles(String[] titles) {
        this.titles = titles;
    }

    public int[] getColors() {
        return colors;
    }

    public void setColors(int[] colors) {
        this.colors = colors;
    }

    public PointStyle[] getPointStyles() {
        return pointStyles;
    }

    public void setPointStyles(PointStyle[] pointStyles) {
        this.pointStyles = pointStyles;
    }

    public int[] getShapeTypes() {
        return shapeTypes;
    }

    public void setShapeTypes(int[] shapeTypes) {
        this.shapeTypes = shapeTypes;
    }

    public int getAxesColor() {
        return axesColor;
    }

    public void setAxesColor(int axesColor) {
        this.axesColor = axesColor;
    }

    public int getLabelsColor() {
        return labelsColor;
    }

    public void setLabelsColor(int labelsColor) {
        this.labelsColor = labelsColor;
    }

    public int getMarginsColor() {
        return marginsColor;
    }

    public void setMarginsColor(int marginsColor) {
        this.marginsColor = marginsColor;
    }

    public void setXYVaules(Object[][] xyVaules) {
        this.xyVaules = xyVaules;
    }

    public Object[][] getXyVaules() {
        return xyVaules;
    }

    public void setXyVaules(Object[][] xyVaules) {
        this.xyVaules = xyVaules;
    }

    public void setItemOnSelectedListener(
            ItemOnSelectedListener itemOnSelectedListener) {
        this.itemOnSelectedListener = itemOnSelectedListener;
    }

    public ItemOnSelectedListener getItemOnSelectedListener() {
        return itemOnSelectedListener;
    }

    public void setSounds(String[] sounds) {
        this.sounds = sounds;
    }

    public String[] getSounds() {
        return sounds;
    }

    public void setDescs(String[] descs) {
        this.descs = descs;
    }

    public String[] getDescs() {
        return descs;
    }

//	public boolean isSoundAutoPlay() {
//		return soundAutoPlay;
//	}
//
//	public void setSoundAutoPlay(boolean soundAutoPlay) {
//		this.soundAutoPlay = soundAutoPlay;
//	}

    public boolean isMaxRange() {
        return isMaxRange;
    }

    public void setMaxRange(boolean isMaxRange) {
        this.isMaxRange = isMaxRange;
    }

    public double[] getInitialRange() {
        if (isSingle()) {
            //不需要重新计算范围，在此类外部赋值。
        }else{
            //多条记录每次重新计算xy值范围。
            computeMaxXYRange();
        }
        return new double[]{xInitialMinAge, xInitialMaxAge, yInitialMinDouble, yInitialMaxDouble};
    }


    /**
     * 计算混合图标最大的XY坐标范围
     */
    public void computeMaxXYRange() {
        Object[][] xyValues = getXyVaules();
        double xMin = Double.MAX_VALUE;
        double xMax = Double.MIN_VALUE;
        double yMin = Double.MAX_VALUE;
        double yMax = Double.MIN_VALUE;
        for (int i = 0; i < xyValues.length; i++) {
            double[] xValue = (double[]) xyValues[i][0];
            double[] yValue = (double[]) xyValues[i][1];
            if (isShow[i]) {
                for (int k = 0; k < xValue.length; k++) {
                    if (xMin > xValue[k]) {
                        xMin = xValue[k];
                    }
                    if (xMax < xValue[k]) {
                        xMax = xValue[k];
                    }
                }
                for (int k = 0; k < yValue.length; k++) {
                    if (yMin > yValue[k]) {
                        yMin = yValue[k];
                    }
                    if (yMax < yValue[k]) {
                        yMax = yValue[k];
                    }
                }
            }
        }
        if (yMin > 0) {
            yMin = 0;
        }
        int xMinInt = (int) xMin;
        int xMaxInt = (int) xMax;

        //计算初始范围,有22个间隔
        xInitialMinAge = xMinInt - intervalAge;
        int xCount = EbtIndemnifyA.XLabels;
        if (xInitialMinAge < 0) {
            xInitialMinAge += intervalAge;
        }
        xInitialMaxAge = xInitialMinAge + intervalAge * xCount;
//        if (xInitialMaxAge > xMaxInt) {
//            xInitialMaxAge = xMaxInt + 1;
//        }
        //计算最大范围
//		double xAgeWidth = xMax - xMin;
//		double intervalDouble = xAgeWidth/intervalAge;
//		int interval = (int) Math.floor(intervalDouble);
//		if(xAgeWidth%intervalAge!=0){
//			interval +=1;
//		}
        xRangeMinDouble = xInitialMinAge;
//		for(int i=0;i<interval;i++){
//			if(xRangeMinDouble-intervalAge>=0){
//				xRangeMinDouble -= intervalAge;
//			}else{
//				break;
//			}
//		}
        Log.d("computeMaxXYRange", "the xRangeMinDouble is " + xRangeMinDouble);
//		if(xRangeMinDouble<0){
//			xRangeMinDouble +=intervalAge;
//		}

        xRangeMaxDouble = xMaxInt;
//		xRangeMaxDouble = xMaxInt + EbtIndemnifyA.XLabels*intervalAge;
        Log.d("computeMaxXYRange", "the xRangeMaxDouble is " + xRangeMaxDouble);


//		xInitialMinAge = xMinInt;
//		xInitialMaxAge = xMaxInt;
//		xRangeMinDouble = xMinInt;
//		xRangeMaxDouble = xMaxInt;

        yInitialMinDouble = yMin;
        yInitialMaxDouble = yMax * 1 * 1.1;
        yRangeMinDouble = 0;
        yRangeMaxDouble = yMax * 1 * 1.1;

//		initialRange = new double[] { xMinInt - 1, xMaxInt + 1, yMin, yMax * 1 * 1.1 };
//		double[] range2 = new double[] { xMinInt - 1, 105, -0, yMax * 2 * 1.1 };
//		range = new double[] { xMinInt - 1, xMaxInt+1, -0, yMax * 1 * 1.1 };
//		Log.i("my", "range:" + range + ", range2:" + range);
//		Log.i("my", "xMin:" + xMin + ",xMax:" + xMax + ",yMin:" + yMin
//				+ ",yMax:" + yMax);
    }

    public double[] getRange() {
        return new double[]{xRangeMinDouble, xRangeMaxDouble, yRangeMinDouble, yRangeMaxDouble};
    }

    public boolean isSingle() {
        return isSingle;
    }

    public void setSingle(boolean isSingle) {
        this.isSingle = isSingle;
    }

    public boolean[] getIsShow() {
        return isShow;
    }

    public void setIsShow(boolean isShow[]) {
        this.isShow = isShow;
    }

    public int getBarSpacing() {
        return barSpacing;
    }

    public void setBarSpacing(int barSpacing) {
        this.barSpacing = barSpacing;
    }

    /**
     * 返回x坐标刻度数
     *
     * @return
     */
    public int getxLabels() {
        int xLablesCount = (xInitialMaxAge - xInitialMinAge) / intervalAge;
        if ((xInitialMaxAge - xInitialMinAge) % intervalAge > 0) {
            xLablesCount++;
        }
        xLabels = xLablesCount;
        return xLabels;
    }

    public void setxLabels(int xLabels) {
        this.xLabels = xLabels;
    }

    public int getxInitialMinAge() {
        return xInitialMinAge;
    }

    public void setxInitialMinAge(int xInitialMinAge) {
        this.xInitialMinAge = xInitialMinAge;
    }

    public int getxInitialMaxAge() {
        return xInitialMaxAge;
    }

    public void setxInitialMaxAge(int xInitialMaxAge) {
        this.xInitialMaxAge = xInitialMaxAge;
    }

    public double getxRangeMinDouble() {
        return xRangeMinDouble;
    }

    public void setxRangeMinDouble(double xRangeMinDouble) {
        this.xRangeMinDouble = xRangeMinDouble;
    }

    public double getxRangeMaxDouble() {
        return xRangeMaxDouble;
    }

    public void setxRangeMaxDouble(double xRangeMaxDouble) {
        this.xRangeMaxDouble = xRangeMaxDouble;
    }


    public double getyRangeMinDouble() {
        return yRangeMinDouble;
    }

    public void setyRangeMinDouble(double yRangeMinDouble) {
        this.yRangeMinDouble = yRangeMinDouble;
    }

    public double getyRangeMaxDouble() {
        return yRangeMaxDouble;
    }

    public void setyRangeMaxDouble(double yRangeMaxDouble) {
        this.yRangeMaxDouble = yRangeMaxDouble;
    }

    public double getyInitialMinDouble() {
        return yInitialMinDouble;
    }

    public void setyInitialMinDouble(double yInitialMinDouble) {
        this.yInitialMinDouble = yInitialMinDouble;
    }

    public double getyInitialMaxDouble() {
        return yInitialMaxDouble;
    }

    public void setyInitialMaxDouble(double yInitialMaxDouble) {
        this.yInitialMaxDouble = yInitialMaxDouble;
    }

    public int getIntervalAge() {
        return intervalAge;
    }

    public void setIntervalAge(int intervalAge) {
        this.intervalAge = intervalAge;
    }

    /**
     * 获取所有利益年龄的最小和最大值
     *
     * @return
     */
    public int[] getAgeMinMaxValue() {
        Object[][] xyValues = getXyVaules();
        double xMin = Double.MAX_VALUE;
        double xMax = Double.MIN_VALUE;
        double yMin = Double.MAX_VALUE;
        double yMax = Double.MIN_VALUE;
        for (int i = 0; i < xyValues.length; i++) {
            double[] xValue = (double[]) xyValues[i][0];
            double[] yValue = (double[]) xyValues[i][1];
            for (int k = 0; k < xValue.length; k++) {
                if (xMin > xValue[k]) {
                    xMin = xValue[k];
                }
                if (xMax < xValue[k]) {
                    xMax = xValue[k];
                }
            }
            for (int k = 0; k < yValue.length; k++) {
                if (yMin > yValue[k]) {
                    yMin = yValue[k];
                }
                if (yMax < yValue[k]) {
                    yMax = yValue[k];
                }
            }
        }
        if (yMin > 0) {
            yMin = 0;
        }
        int[] ageMinMax = new int[2];
        ageMinMax[0] = (int) xMin;
        ageMinMax[1] = (int) xMax;
        return ageMinMax;
    }

    public int[] getMinMaxValue(float textSize) {
        Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
        paint.setTextSize(textSize);
        Object[][] xyValues = getXyVaules();
        double yMin = Double.MAX_VALUE;
        double yMax = Double.MIN_VALUE;
        String title = "";
        int maxValue = 0;
        String titles[] = getTitles();
        for (int i = 0; i < xyValues.length; i++) {
            double[] yValue = (double[]) xyValues[i][1];
            for (int k = 0; k < yValue.length; k++) {
                if (yMin > yValue[k]) {
                    yMin = yValue[k];
                }
                if (yMax < yValue[k]) {
                    yMax = yValue[k];
                }
            }
            if (paint.measureText(title) < paint.measureText(titles[i])) {
                title = titles[i];
                maxValue = i;
            }

        }
        if (yMin > 0) {
            yMin = 0;
        }
        int[] ageMinMax = new int[2];
        ageMinMax[0] = (int) maxValue;
        ageMinMax[1] = (int) yMax;
        return ageMinMax;
    }


    public boolean isShowFloat() {
        return isShowFloat;
    }

    public void setShowFloat(boolean isShowFloat) {
        this.isShowFloat = isShowFloat;
    }

}
